/*
 * Copyright:  2018 smarabbit studio.
 *
 * Licensed under the Confluent Community License; you may not use this file except in
 * compliance with the License.  You may obtain a copy of the License at
 *
 * http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under
 *  the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 *  either express or implied.  See the License for the specific language governing
 *  permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:    2020/7/11 上午8:59
 *
 */

package com.massyframework.beanskin.spring.beancontext.lazyinit;

import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
import org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader;
import org.w3c.dom.Element;

/**
 *
 */
public class LazyInitConfigurerBeanDefinitionDocumentReader extends DefaultBeanDefinitionDocumentReader {

    private String defaultLazyInit = "default";

    public LazyInitConfigurerBeanDefinitionDocumentReader() {
    }

    /**
     * Register each bean definition within the given root {@code <beans/>} element.
     *
     * @param root
     */
    @Override
    protected void doRegisterBeanDefinitions(Element root) {
        String lazyInit = root.getAttribute(BeanDefinitionParserDelegate.DEFAULT_LAZY_INIT_ATTRIBUTE);
        if (lazyInit.equals("default")){
            if (!lazyInit.equals(this.defaultLazyInit)){
                root.setAttribute(BeanDefinitionParserDelegate.DEFAULT_LAZY_INIT_ATTRIBUTE, this.defaultLazyInit);
            }
        }
        super.doRegisterBeanDefinitions(root);
    }

    /**
     * 缺省的延迟加载
     * @return {@link }
     */
    public String getDefaultLazyInit() {
        return defaultLazyInit;
    }

    /**
     * 设置缺省延迟加载值
     * @param defaultLazyInit
     */
    public void setDefaultLazyInit(String defaultLazyInit) {
        this.defaultLazyInit = defaultLazyInit;
    }
}
